package net.expectx.gw.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.expectx.gw.bean.ContentArticle;
import net.expectx.gw.bean.CustomerRequirement;
import net.expectx.gw.service.CustomerRequirementService;
import net.expectx.gw.util.AdminConstants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/manager/customer/requirement")
@Api(value = "控制器", description = "管理")
public class CustomerRequirementController extends BaseController {

    @Autowired
    CustomerRequirementService customerRequirementService;
    @ApiOperation(value = "需求主页")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(String type, ModelMap modelMap) {
        modelMap.put("type", type);
        return "customerRequirement/index";
    }
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "1", value = "page") int page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            HttpServletRequest request
    ) {
        String strWhere = "1=1 and is_deleted=0 ";
        String strOrder = null;
        String name = request.getParameter("name");
        if (StringUtils.isNotBlank(name)) {
            strWhere += " and name like('%" + name + "%')";
        }
        List<CustomerRequirement> customerRequirements = customerRequirementService.findByPage(page, limit, strWhere, strOrder);
        long total = customerRequirementService.count(strWhere);
        Map<String, Object> result = new HashMap<>(16);
        result.put("code", AdminConstants.LAYUI_DATA_OK);
        result.put("count", total);
        result.put("msg", "");
        result.put("data", customerRequirements);
        return result;
    }
}
